package com.appspot.zarm.gomoku.action;

import com.appspot.zarm.controller.GameAction;
import com.appspot.zarm.game.BoardStorage;
import com.appspot.zarm.game.Player;
import com.appspot.zarm.game.Rival;
import com.appspot.zarm.gomoku.Coord;
import com.appspot.zarm.gomoku.GomokuBoard;
import com.appspot.zarm.gomoku.GomokuGameFacade;
import com.appspot.zarm.gomoku.transfer.JsonTransfer;

public class GomokuAction implements GameAction{

	private static final int BOARD_SIZE = 15;
	
	public static final String TURN_ACTION = "turn";
	public static final String START_ACTION = "start";
	public static final String FINISH_ACTION = "finish";
	
	@Override
	public String process(BoardStorage boardStorage, Player player,
			Rival rival, String action, String data) {
		
		if (action.equals(START_ACTION)) {
			 boardStorage.putBoard(player, rival, new GomokuBoard(BOARD_SIZE) );
		} else if (action.equals(FINISH_ACTION)) {
			boardStorage.removeBoard(player);
		} else if (action.equals(TURN_ACTION)) {
			GomokuBoard board = (GomokuBoard) boardStorage.getBoard(player);
			GomokuGameFacade gomokuGameFacade = new GomokuGameFacade(player, board);
			Coord position = JsonTransfer.getCoordFromJson(data);
			gomokuGameFacade.doTurn(position);
			return JsonTransfer.getBoardToJson(board);
		}
		return null;
	}

}
